import api from '../../http/apis/api';
export default {
    namespaced: true,
    state: {
        ruleForm: {
            adminName: "1",
            password: "1",
            adminsPass: "",
            checkAdminsPass: "",
            adminsName: "",
            newadminsPass: "",
            newcheckAdminsPass: "",
            newadminsName: "",
        },
        rules: {
            name: [
                { required: true, message: "请输入账户名", trigger: "blur" },
                { min: 1, max: 5, message: "长度在 1 到 5 个字符", trigger: "blur" },
            ],
            pass: [
                { required: true, message: "请输入密码", trigger: "blur" },
                {
                    min: 1,
                    max: 12,
                    message: "长度在 1 到 12 个字符",
                    trigger: "blur",
                },
            ],
        },
        power: 2,
        adminsData: [],
        _id: ""
    },
    mutations: {
        //忘记密码·
        forgetPwd(state) {
            console.log('忘记密码');
        },
        //设置当前登录的人的权限等级
        setPower(state, newPower) {
            state.power = newPower;
        },
        //设置
        setAdminsData(state, newData) {
            state.adminsData = newData;
        },
        //设置_id
        set_id(state,info){
            state.ruleForm.newadminsName = info.adminName;
            state.ruleForm.newadminsPass = info.password;
            state.ruleForm.newcheckAdminsPass = info.password;
            state._id = info._id
        },
        //重置
        reset(state){
            state.ruleForm.adminsName = "";
            state.ruleForm.adminsPass = "";
            state.ruleForm.checkAdminsPass = "";
            state.ruleForm.newadminsName = "";
            state.ruleForm.newadminsPass = "";
            state.ruleForm.newcheckAdminsPass = "";
        }

    },
    actions: {
        //登录验证
        async login(context) {
            const { data } = await api.admins.login({
                adminName: context.state.ruleForm.adminName,
                password: context.state.ruleForm.password,
            });
            console.log(data);
            context.commit('setPower', data.data);
            return data;
        },
        //添加管理员
        async addAdmins(context) {
            const { data } = await api.admins.addAdmins({
                adminName: context.state.ruleForm.adminsName,
                password: context.state.ruleForm.adminsPass
            });
            return data;
        },
        //获取所有的管理员
        async getAdmins(context) {
            const { data } = await api.admins.getAdmins();
            context.commit('setAdminsData', data.data)
        },
        //删除
        async deleteAdmins(context, _id) {
            const { data } = await api.admins.deleteAdmins(_id);
            return data;
        },
        //更改管理员信息
        async updataAdmins(context, state_radio) {
            const { data } = await api.admins.updataAdmins({
                _id: context.state._id,
                adminName: context.state.ruleForm.newadminsName,
                password: context.state.ruleForm.newadminsPass,
                state: state_radio
            });
            return data
        }
    }
}